import { View, Text,Image,Picker,AtList } from '@tarojs/components'
import { AtInput, AtForm,AtTag,AtToast } from 'taro-ui'
import { useState,useEffect } from 'react'
import Taro, { getCurrentInstance } from '@tarojs/taro'
import HTTP from '../../service/api'
import './addAddress.less'
const Addaddress=()=>{
    const dd=JSON.parse(Taro.getStorageSync('infodata')) 
    const [info,setInfo]=useState(dd)

    const [region,setSelector]=useState(['广东省', '广州市', '海珠区'])
    const [customItem]=useState('全部')
    const bindRegionChange=(e)=>{
        setSelector(e.target.value)
    }
    const [state,setState]=useState(true)
    const setting=(ev)=>{
        setState(!state)
    }
    const setpop=(name,e)=>{
        if(name=='username'){
            setInfo({...info,username:e});
        }else if(name=='phone'){
            setInfo({...info,phone:e})
        }else if(name=='shippingAddress'){
            setInfo({...info,shippingAddress:e})
        }
    }
    const [tit,settit]=useState('')
    useEffect(()=>{
        const tit=getCurrentInstance().router.params.name;
        settit(tit)
    })
    

    const insert=()=>{
        console.log(state);
        Taro.request({
            url:HTTP.api2+'/userAddress/updateUserAddress',
            method:'POST',
            header:{
                'content-type':'application/x-www-form-urlencoded'
            },
            data:{
                uid:info.uid,
                username:info.username,
                phone:info.phone,
                defaultAddress:state,
                id:info.id,
                shippingAddress:info.shippingAddress
            }
        }).then(res=>{
            console.log(res);
            if (res.data.code=='200') {
                <AtToast  text="修改成功" status="success" hasMask></AtToast>
                Taro.navigateTo({
                    url:'/pages/address/address'
                })
            }else{
                <AtToast  text="修改失败" status="error" hasMask></AtToast>
            }
        })

    }
    
    return(
        <View className='addBox'>
            <View style={{fontSize:'30px',fontWeight:'bold'}}>{tit}</View>
            <AtForm className='addInfo' style={{border:'none'}}>
                <AtInput
                    name='username'
                    title='联系人:'
                    type='text'
                    placeholder='联系人名称'
                    value={info.username}
                    onChange={setpop.bind(this,'username')}
                />
                <AtInput
                    name='phone'
                    title='电        话:'
                    type='phone'
                    placeholder='一定要填喔~'
                    value={info.phone}
                    onChange={setpop.bind(this,'phone')}
                />
                 <View className='pick'>
                    <picker mode="region" onChange={bindRegionChange} value={region} onColumnChange={customItem}>
                        <view class="picker">
                        地    址：{region[0]},{region[1]},{region[2]}
                        </view>
                    </picker>
                 </View>
                <AtInput
                    name='shippingAddress'
                    title='详   情:'
                    type='text'
                    placeholder='记得把住的地址写清楚喔~'
                    value={info.shippingAddress}
                    onChange={setpop.bind(this,'shippingAddress')}
                />
            </AtForm>
            <Text style={{fontSize:'16px',fontWeight:'bold'}}>设置默认</Text>
            <AtTag size='small' onClick={setting} className={state?'at-tag':'at-tag1'}>默认</AtTag>
            <view className='addDZ' onClick={insert}>{tit}</view>
        </View>
    )
}
export default Addaddress;